package com.boshao.classschedule.fragment;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.activity.MainActivity;
import com.boshao.classschedule.model.DaoMaster;
import com.boshao.classschedule.model.DaoMaster.DevOpenHelper;
import com.boshao.classschedule.model.DaoSession;
import com.boshao.classschedule.model.LifeScheduleDao;
import com.boshao.classschedule.model.classschedule.ClassDaoMaster;
import com.boshao.classschedule.model.classschedule.ClassDaoMaster.ClassDevOpenHelper;
import com.boshao.classschedule.model.classschedule.ClassDaoSession;
import com.boshao.classschedule.model.classschedule.ClassScheduleDao;
import com.boshao.classschedule.widget.ActionBarLeft;
import com.boshao.classschedule.widget.ActionBarRight;

/**
 * 抽象Fragment,由子类继承.该类提供ActionBar
 * @author boshao
 *
 */
public abstract class AbstartFragment extends Fragment{
	protected View mParentView;
	protected RelativeLayout mRlActionBar;	
	protected abstract String getFragmentTag();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}
	
	protected View onAppCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
		return null;
	}
	
	protected int getContentLayoutId(){
		return -1;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if(mParentView != null){
			return mParentView;
		}
		//加载actionbar的view
		mRlActionBar = (RelativeLayout) inflater.inflate(R.layout.actionbar,container,false);
		
		//onAppCreateView()由子类继承,获取子类view
		mParentView = onAppCreateView(inflater,container,savedInstanceState);
		
		//子类Fragment的顶层view需要时RelativeLayout,否则会报错
		if(mParentView instanceof RelativeLayout){
			//getContentLayoutId():由子类实现,如果没有实现则没有ActionBar,否则有
			if(getContentLayoutId() != -1){
				RelativeLayout relativeLayout = (RelativeLayout) mParentView.findViewById(getContentLayoutId());
				relativeLayout.addView(mRlActionBar);
				setupActionBar(mRlActionBar);
			}			
		}else{
			throw new IllegalStateException("child view must be relativelayout");
		}
		return mParentView;
	}
	
	protected String getActionBarLeftTitle(){
		return null;
	}
	
	protected ActionBarLeft getActionBarLeft(){
		return ActionBarLeft.BACK;
	}
	
	protected ActionBarRight getActionBarRight(){
		return ActionBarRight.ALL;
	}
	
	protected String getTitle(){
		return null;
	}
	
	protected void onClickActionLeft(){
	}
	
	protected void onClickActionRight(int resId){
	}
	
	private static final int[] REDIS = {R.id.action_right_one,R.id.action_right_two,R.id.actionbar_right_three};
	
	/**
	 * 初始化ActionBar
	 * @param mRlActionBar
	 */
	private void setupActionBar(RelativeLayout mRlActionBar) {
		TextView tvLeft = (TextView) mRlActionBar.findViewById(R.id.actionbar_left_back);
		//getActionBarLeft():由子类继承,可以实现是否显示左边Actionbar的返回箭头
		if(getActionBarLeft() == ActionBarLeft.NULL){
			tvLeft.setVisibility(View.GONE);//隐藏左边Actionbar的返回箭头
		}else{
			tvLeft.setVisibility(View.VISIBLE);
			tvLeft.setCompoundDrawablesWithIntrinsicBounds(getActionBarLeft().getDrawableResId(),0,0,0);
			if(!TextUtils.isEmpty(getActionBarLeftTitle())){
				tvLeft.setText(getActionBarLeftTitle());
			}
			tvLeft.setOnClickListener(mActionBarOnClickListener);
		}
		
		//右边actionbar
		LinearLayout ivRight = (LinearLayout)mRlActionBar.findViewById(R.id.ll_action_right);
		//getActionBarRight():由子类实现,需要显示几个图标(右边的actionbar)
		int[] resIds = getActionBarRight().getDrawableResId();
		for (int i = 0; resIds.length > 0 && i < resIds.length; i++) {
			ImageView iv = (ImageView) ivRight.findViewById(REDIS[i]);
			iv.setVisibility(View.VISIBLE);
			iv.setImageResource(resIds[i]);
			iv.setOnClickListener(mActionBarOnClickListener);
		}
		if(!TextUtils.isEmpty(getTitle())){
			TextView tvTitle = (TextView) mRlActionBar.findViewById(R.id.actionbar_title);
			tvTitle.setText(getTitle());
		}
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		removeRootView();
	}
	
	private void removeRootView() {
		if(mParentView != null){
			ViewGroup viewGroup = (ViewGroup) mParentView.getParent();
			viewGroup.removeView(mParentView);
		}
	}

	
	protected static  final int NO_ANIMATION = 0;
	protected static final int HAS_ANIMATION = 1;
	
	protected int getAnimationStyle(){
		return HAS_ANIMATION;
	}
	
	//Fragment切换动画
	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		Animation animation = null;
		int style = getAnimationStyle();
		switch (style) {
			case NO_ANIMATION:
				animation = super.onCreateAnimation(transit, enter, nextAnim);
				break;
			case HAS_ANIMATION:
				animation = AnimationUtils.loadAnimation(getActivity(),enter?R.anim.fragment_show_in_right:R.anim.fragment_hide_in_right);
				break;
			default:
				animation = super.onCreateAnimation(transit, enter, nextAnim);
				break;
		}	
		return animation;
	}
	
	private ActionBarOnClickListener mActionBarOnClickListener = new ActionBarOnClickListener();
	private class ActionBarOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			int resId = 0;
			switch (v.getId()) {
				case R.id.actionbar_left_back:
					onClickActionLeft();//左边返回Actionbar的事件监听，由子类实现
					break;
				case R.id.action_right_one:
					resId = R.id.action_right_one;
					break;
				case R.id.action_right_two:
					resId = R.id.action_right_two;
					break;
				case R.id.actionbar_right_three:	
					resId = R.id.actionbar_right_three;								
					break;				
			}
			if(resId > 0){
				onClickActionRight(resId);//右边返回Actionbar的事件监听，由子类实现
			}
			
		}
	}
	
	public final MainActivity getMainActivity(){
		return getActivity() != null? (MainActivity)getActivity() : null;
	}
	
	protected boolean onBackPressed(){
		getMainActivity().onBackPressed();
		return false;
	}
	
	/**
	 * 隐藏软键盘
	 * @param iBinder
	 */
	protected void hideSoftInput(IBinder iBinder){
		InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(iBinder, 0);
	}
	
	/**
	 * 获取日程数据库操作类:写操作
	 * @return
	 */
	public LifeScheduleDao writeLifeScheduleDao(){
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(getActivity(),"time-manager-db",null);
		SQLiteDatabase db = helper.getWritableDatabase();
		DaoMaster daoMaster = new DaoMaster(db);
		DaoSession daoSession = daoMaster.newSession();
		LifeScheduleDao dao = daoSession.getLifeScheduleDao();
		return dao;
	}
	
	/**
	 * 获取日程数据库操作类:读操作
	 * @return
	 */
	public LifeScheduleDao readLifeScheduleDao(){
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(getActivity(),"time-manager-db",null);
		SQLiteDatabase db = helper.getReadableDatabase();
		DaoMaster daoMaster = new DaoMaster(db);
		DaoSession daoSession = daoMaster.newSession();
		LifeScheduleDao dao = daoSession.getLifeScheduleDao();
		return dao;
	}
	
	/**
	 * 获取课程数据库操作类:写操作
	 * @return
	 */
	public ClassScheduleDao writeClassScheduleDao(){
		ClassDevOpenHelper helper = new ClassDaoMaster.ClassDevOpenHelper(getActivity(),"class-schedule-db",null);
		SQLiteDatabase db = helper.getWritableDatabase();
		ClassDaoMaster daoMaster = new ClassDaoMaster(db);
		ClassDaoSession daoSession = daoMaster.newSession();
		ClassScheduleDao dao = daoSession.getClassScheduleDao();
		return dao;
	}
	
	/**
	 * 获取课程数据库操作类:读操作
	 * @return
	 */
	public ClassScheduleDao readClassScheduleDao(){
		ClassDevOpenHelper helper = new ClassDaoMaster.ClassDevOpenHelper(getActivity(),"class-schedule-db",null);
		SQLiteDatabase db = helper.getReadableDatabase();
		ClassDaoMaster daoMaster = new ClassDaoMaster(db);
		ClassDaoSession daoSession = daoMaster.newSession();
		ClassScheduleDao dao = daoSession.getClassScheduleDao();
		return dao;
	}
}
